/**
 *  Copyright 2002-2009 the original author or authors.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package foo.bar.blog.utils;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationContext;

/**
 * A FactoryBean that serves as a factory for another bean referenced by it's name
 * (through the beanName property) Eg. with
 *
 * <code>
 * <bean name="encryptor" class="foo.bar.utils.BeanRef">
 *    <property name="beanName" value="simpleEntryptor" />
 * </bean>
 *
 * <bean name="simpleEncryptor" class="foo.bar.core.security.encryption.SimpleEncryptor"/>
 * <bean name="md2Encryptor" class="foo.bar.core.security.encryption.Md2Encryptor" />
 * <bean name="md5Encryptor" class="foo.bar.core.security.encryption.Md5Encryptor" />
 * <bean name="sha1Encryptor" class="foo.bar.core.security.encryption.Sha1Encryptor" />
 * <bean name="sha256Encryptor" class="foo.bar.core.security.encryption.Sha256Encryptor" />
 * <bean name="sha384Encryptor" class="foo.bar.core.security.encryption.Sha384Encryptor" />
 * <bean name="sha512Encryptor" class="foo.bar.core.security.encryption.Sha512Encryptor" />
 *
 * <bean name="userService" class="foo.bar.core.services.UserService">
 *    <property name="encryptor" ref="encryptor" />
 * </bean>
 * </code>
 *
 * bean "userService" is refering to {@link BeanRef} named "encryptor which is really a
 * {@link foo.bar.blog.core.security.encryption.SimpleEncryptor} named "simpleEncruptor". If we were
 * to latter on switch the implementation to eg. Md5Encryptor, all we'd need to do is to change
 *
 * <code>
 * <bean name="encryptor" class="foo.bar.utils.BeanRef">
 *    <property name="beanName" value="simpleEntryptor" />
 * </bean>
 * </code>
 *
 * To
 * <code>
 * <bean name="encryptor" class="foo.bar.utils.BeanRef">
 *    <property name="beanName" value="md5Encryptor" />
 * </bean>
 * </code>
 *
 * @author tmjee
 * @version $Date: 2009-03-14 06:53:07 +0000 (Sat, 14 Mar 2009) $ $Id$
 */
public class BeanRef implements FactoryBean, ApplicationContextAware {

    private ApplicationContext applicationContext;
    private String beanName;

    public void setBeanName(String beanName) {
        this.beanName = beanName;
    }

    public Object getObject() throws Exception {
        return applicationContext.getBean(beanName);
    }

    public Class getObjectType() {
        return applicationContext.getBean(beanName).getClass();
    }

    public boolean isSingleton() {
        //return ((FactoryBean)applicationContext.getBean("&"+beanName)).isSingleton();
        return true;
    }

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
